<template>
  <el-dialog v-if="load" title="批量添加SKU" v-model="visible" append-to-body :close-on-click-modal="false" width="800px"
    :z-index="2000" @close="handleClose">
    <el-alert :closable="false" class="mb-3">
      <div>1. 一行一个（从Excel中复制）</div>
      <div>2. 严格按照格式排列，不要打乱顺序!!!</div>
      <div>3. 格式:SKU/自定义SKU（Tab键）国家（Tab键）销售（Tab键）销售SKU数量</div>
    </el-alert>
    <el-input ref="input" v-model="form.sku_content" type="textarea" :autosize="{ minRows: 2, maxRows: 8 }"
      placeholder="请输入" @keydown.tab.prevent="addTabCharacter" />
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">添 加</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../../api'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})

const emit = defineEmits(['close', 'change'])

const inputRef = ref(null)
const addTabCharacter = (event) => {
  if (event.keyCode === 9) {
    event.preventDefault();
    form.value.sku_content += '\t';
  }
}

// 打开弹窗
const open = async (selection) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {}
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}

// 提交
const submit = () => {
  loadings.submit = true
  api.batchAddSku(form.value).then(res => {
    emit('change', res.data)
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>../../api